package com.k.demo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.k.demo.entity.RedBag;
import com.k.demo.entity.RedBagRecord;

@Mapper
public interface RedBagRecordMapper {
	
	//找全部
	@ResultMap("RedBagRecordMap")
	@Select("select * from red_bag_record where user_id=#{user_id} and is_delete =1")
	List<RedBagRecord> selectAll(int user_id);
	
	//1是未使用  2是已使用  3是已过期  到时前端要对应好
	@ResultMap("RedBagRecordMap")
	@Select("select * from red_bag_record where red_bag_state = #{state} and user_id=#{user_id} and is_delete =1")
	List<RedBagRecord> selectByState(int state,int user_id);
	
	//使用红包
	//修改红包的记录
	@Update("update red_bag_record set red_bag_state=#{state},use_time=sysdate where user_id=#{user_id} and red_bag_record_id=#{red_bag_record_id}")
	int updateState(int state,int user_id,int red_bag_record_id);
	
	//领取了红包（传入红包记录对象，新建一条红包记录）
	int addRedBagRecord(RedBagRecord redBagRecord);
	
	//查询当前用户下的所有红包
	@Select("select * from red_bag_record where user_id=#{user_id} and is_delete = 1")
	List<RedBagRecord> selectByUserId(int user_id);
	
	//传入红包对象
	List<RedBagRecord> selectByRecord(RedBagRecord redBagRecord);
	
	//
	@Select("select * from red_bag_record and is_delete = 1")
	List<RedBagRecord> getAllUserRecoed();
	
	//查找当天使用的红包总额
	@Select("select nvl(sum(face_value),0) from red_bag_record  left join red_bag  on red_bag_record.red_bag_id = red_bag.red_bag_id where trunc(red_bag_record.use_time)=to_date(#{date},'yyyy-mm-dd') and red_bag_record.is_delete =1 and red_bag_record.red_bag_state=62")
	Double getTotalValueByDate(String date);
	
	//统计当前用户拥有的红包数量(未删除  并且可使用)
	@Select("select count(red_bag_record_id) from red_bag_record where user_id=#{user_id} and red_bag_state=61 and is_delete=1 ")
	int getUserRedBagRecordAmount(int user_id);
	
	
}
